package com.itheima.project.web;

import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.dict.DataDictEnum;
import com.itheima.project.face.IndexFace;
import com.itheima.project.utils.ResponseWrapBuild;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 索引库controller
 * @author itheima
 * @date 2022/3/5 17:34
 */
@Api(tags = "ES索引库controller")
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IndexFace indexFace;

    /**
     * 索引库是否存在
     */
    @GetMapping("exist")
    @ApiOperation(value = "判断索引库是否存在",notes = "判断索引库是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
    })
    public ResponseWrap<Boolean> isExistIndex(@RequestParam(value = "indexName") String indexName){
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,indexFace.existIndex(indexName));
    }

    /**
     * 创建索引库
     */
    @PutMapping("{indexName}")
    @ApiOperation(value = "创建索引库",notes = "创建索引库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
            @ApiImplicitParam(name = "source",value = "创建所有库的资源请求",required = true,dataType = "json"),
    })
    public ResponseWrap<Boolean> createIndex(@PathVariable(value = "indexName") String indexName,@RequestBody String source){
        indexFace.createIndex(source,indexName);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,true);
    }

    /**
     * 删除索引库
     */
    @DeleteMapping("{indexName}")
    @ApiOperation(value = "删除索引库",notes = "删除索引库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexName",value = "索引库名称",required = true,dataType = "String"),
    })
    public ResponseWrap<Boolean> deleteIndex(@PathVariable(value = "indexName") String indexName){
        indexFace.deleteIndex(indexName);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,true);
    }

}
